Repository navigation
fix: close a document's pages, fonts and XObjects before the document (ADR-025) - #66
Merged
Merged
Conversation
pdf_doc_close() closed the document and left its pages open. Page shims
checked only the page handle, so rendering or extracting text from a
page whose document had been closed read the freed CPDF_Document, and
text extraction also wrote into its indirect-object map. A page
collected later ran FPDF_ClosePage after FPDF_CloseDocument. Font and
XObject handles likewise outlived their document, and the XObject shim
never checked that its document was open.
The ADR-024 annotation registry becomes src/handle_registry.{h,cpp}, a
per-document registry of every handle PDFium closes for a document:
annotation contexts, pages, fonts and XObjects, each minted by one
make_*_handle() and cleared by one release_*_handle(). Closing a
document releases the annotations, then the pages, then the fonts and
XObjects, and only then calls FPDF_CloseDocument. The page handles of
pdf_form_fields() now pin their document like every other page.
Page and page-object calls on a page that its document's close closed
raise an error that says so. pdf_text_new() and
pdf_obj_form_from_xobject() refuse a font or XObject from another
document, and the form object that pdf_obj_form_from_xobject() inserts
pins its page instead of the XObject, so it stays usable after
pdf_xobject_close() as documented.
Records the design as ADR-025 and corrects the architecture vignette,
which said a page stayed usable after pdf_doc_close().
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…close() origin/main now gives the objects of pdf_annot_objects() their real type, and the children of a form among them pin the annotation instead of the page. Build the doc-close test's annotation objects from the inline appearance-stream fixture that came with that change, so all five object types and a form child are refused once their document is closed, at the R and the C++ layer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Regenerated with roxygen2 against a fresh build of this branch. Only the pages whose roxygen source changed differ. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PDFium's headers do not state the order. A page closed after its
document ran clean under valgrind; the order is what PDFium's own
lifetimes assume, not a rule it enforces, and the annotation-only
wording ("releases them only while their document is open") did not
fit pages, fonts or XObjects.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in the drop-CRAN docs (ADR-026) and the licence-notice install. Only the ADR index conflicted: both sides appended a row after ADR-024, so the resolution keeps ADR-025 and ADR-026 in order. Rcpp exports and roxygen output are unchanged by the merge, and the full suite passes under MALLOC_CHECK_=3 (3464 passed). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pdf_doc_close()closed the document at once and left its pages open, and every page shim checked only the page handle. A page used after its document was closed therefore handed PDFium a page whoseCPDF_Documenthad been freed. Under valgrind,pdf_render_page()andpdf_text_runs()read freed document memory. Text extraction also wrote into it: resolving a font's/ToUnicodeinserts into the freed document's indirect-object map. The results still looked right, so nothing noticed. On Windows this is a plausible source of the unexplained crashes. Page-objects read from such a page passed their own check too, and a page collected later ranFPDF_ClosePageafterFPDF_CloseDocument.This extends the ADR-024 registry from annotation contexts to every handle that PDFium closes for a document: pages, fonts and XObjects. The design is recorded as ADR-025.
Changes
src/annot_registry.{h,cpp}becomessrc/handle_registry.{h,cpp}. It is one per-document registry whose entries carry their kind (annotation, page, font, XObject), and it keeps ADR-024's weak entries, reverse map and unconditional deregistration in the finalizer. Each kind has onemake_*_handle()and onerelease_*_handle().close_document_handle()releases the document's annotations, then its pages, then its fonts and XObjects, and only then callsFPDF_CloseDocument. That coverspdf_doc_close(), the document finalizer and thepdf_n_up()finalizer.make_page_handle():cpp_load_page,cpp_page_newandcpp_form_field_handles. The pages ofpdf_form_fields()now pin their document inprot. Before, a droppedpdf_form_fields()list let GC close the document under a page still in use.cpp_font_load_standard,cpp_font_load_truetype,cpp_font_load_cidtype2) and XObjects (cpp_xobject_from_page) are registered. The XObject shim now also requires its document to be open. Before,pdf_obj_form_from_xobject()after the destination document closed built a form object from the freed document.pdf_text_new(font = )andpdf_obj_form_from_xobject()refuse a font or XObject from another document, aspdf_obj_add_existing_mark()already does. A loaded font or an XObject used that way was written out wrongly bypdf_save(), and read freed memory once the other document closed.pdf_obj_form_from_xobject()inserts now pins its page instead of the XObject. It stays usable afterpdf_xobject_close(), as the documentation always said; it used to be refused.cpp_page_insert_object()is folded intocpp_form_obj_from_xobject().cpp_image_icc_profile()validates its page argument, which it never checked.dev/architecture.md.cpp_doc_handle_counts()reports a document's registered handles per kind.User-visible change
Dropping the reference to
doc, without closing it, still leaves its pages usable.Verification
Checked on Linux with R 4.6.1, valgrind 3.22 and PDFium chromium/8066.
Reproducer (the page after
pdf_doc_close()): before the fix, 30 invalid reads and 14 invalid writes into the freed document. After it, none, and each call raises the error above.Weekly valgrind invocation (serial
testthat.R): FAIL 0, PASS 3446.definitely lost: 0 bytes in 0 blocks,indirectly lost: 0 bytes in 0 blocks, and 0 invalid read, write or free. All 521 error contexts are "Mismatched free()" inside libpdfium. The test files changed after the merge ofmainwere re-run under valgrind: 0 failures, 0 invalid accesses, 0 bytes lost.Malloc checking (
MALLOC_CHECK_=3withlibc_malloc_debug): the full suite passes, FAIL 0 and PASS 3450.Crash-isolated object sweep (65 page-object functions × 8 appearance-stream and form targets × 2 teardown orders): 1,040 of 1,040 exit 0, with the same per-call outcomes as before. The same 1,040 calls made after
pdf_doc_close()also all exit 0, and every one is refused.Per handle type, under valgrind (use after the document closed):
pdf_obj_form_from_xobject()Static checks: covr gives 100% R coverage, and lintr reports 0 lints on the touched files. With the pinned styler 1.10.3, no line this branch wrote would be restyled. g++ and clang++ with
-Wall -Wextra -pedanticgive 0 warnings in the touched sources. All vignettes render, and all Rd examples run.Tests
test-document.R: pages, page-objects (including annotation objects and their form children), form-field pages, collection in every order, other documents' pages left open, and registry bookkeeping.test-defensive.R: the C++ shims refuse a closed document's pages and objects.test-font-authoring.R: fonts are closed with their document, and a font from another document is refused.test-api-completion.R: XObjects are closed with their document, and an XObject from another document is refused.pdf_xobject_close().pdf_doc_close().🤖 Generated with Claude Code